# -*- coding: utf-8 -*-

from odoo import models, fields, api, exceptions
# 猎人
class Hunter(models.Model):
    _name="bounty.hunter"

    name=fields.Char(size=20,string=u"姓名" ,required=True)
    vocation=fields.Many2one('bounty.vocation',ondelete='set null',string=u"职阶")
    rank=fields.Many2one('bounty.rank',ondelete='set null',string=u"等级")
    rankVal=fields.Integer(related="rank.power",string=u"基础上限值")
    rankValTrue=fields.Integer(compute="_comput_rank",string=u"职业实际值",store=True)
    gender=fields.Selection([('Male',u'男性'),('Female',u'女性')],string=u"性别")
    tasks=fields.Many2many("bounty.task","hunter_link_th","hunter_id","task_id",string=u"已接任务")
    strong=fields.Integer(string=u"力量")
    speed=fields.Integer(string=u"速度")
    magic=fields.Integer(string=u"法术")


    @api.constrains('strong','speed','magic','rank')
    def _check_Rank(self):
        rankVal=self.strong+self.speed+self.magic
        if rankVal>self.rank.power:
            raise exceptions.ValidationError(u"属性值超过等级上限")

    @api.depends('vocation','strong','speed','magic')
    def _comput_rank(self):
        self.rankValTrue=self.vocation.getPowerTure(strong=self.strong,speed=self.speed,magic=self.speed)



    _sql_constraints=[('name_unique','unique(name)',(u'猎人名已存在'))]




# 任务
class Task(models.Model):
    _name="bounty.task"

    name=fields.Char(size=20,string=u"标题",required=True)
    hunterAction=fields.Text(string=u"内容",required=True)
    money=fields.Float(string=u'赏金',required=True)
    rank=fields.Many2one('bounty.rank',ondelete='set null',string=u"等级")
    hunterNumber=fields.Integer(string=u"组队人数",required=True)
    hunters=fields.Many2many("bounty.hunter","hunter_link_th","task_id","hunter_id",domain="[('rank','=',rank)]",string="组队成员")

    # @api.onchange('rank')
    # def _onchange_equipment(self):
    #     ranker=self.rank.name
    #     return {
    #     'domain':{'hunters':[('rank.name','=',ranker)]}
    #     }
    #
    # @api.model
    # def view_init(self, fields_list):
    #     print "531316531351"





# 等级
class Rank(models.Model):
    _name="bounty.rank"
    _order='power'

    name=fields.Char(string=u"等级",required=True)
    power=fields.Integer(string="等级评定值",required=True)
    hunters=fields.One2many("bounty.hunter","rank",string=u"成员表")
    tasks=fields.One2many("bounty.task","rank",string=u"任务表")







# 职阶
class vocation(models.Model):
    _name="bounty.vocation"

    name=fields.Char(string=u"职阶",required=True)
    hunter_ids=fields.One2many("bounty.hunter","vocation",string=u"成员表")
    strongAdd=fields.Integer(string=u"力量加成倍数",default=1)
    speedAdd=fields.Integer(string=u"速度加成倍数",default=1)
    magicAdd=fields.Integer(string=u"魔法加成倍数",default=1)

    def getPowerTure(self,strong,speed,magic):
        return self.strongAdd*strong+self.speedAdd*speed+self.magicAdd*magic



# class bounty(models.Model):
#     _name = 'bounty.bounty'

#     name = fields.Char()
#     value = fields.Integer()
#     value2 = fields.Float(compute="_value_pc", store=True)
#     description = fields.Text()
#
#     @api.depends('value')
#     def _value_pc(self):
#         self.value2 = float(self.value) / 100
